﻿using System;
using System.Windows;
using System.Windows.Interop;

namespace CustomControls.MessageBox
{
    /// <summary>
    /// 窗体基类
    /// </summary>
    public class MessageBaseWindow : Window, IDisposable
    {
        #region 成员变量

        /// <summary>
        /// 当前窗口句柄
        /// </summary>
        IntPtr wndHandle;

        bool isBusy = false;
        #endregion

        #region 构造函数

        public MessageBaseWindow()
        {
            ResizeMode = ResizeMode.NoResize;
            WindowStyle = WindowStyle.None;
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            ShowInTaskbar = false;
            Closed += new EventHandler(BaseWindow_Closed);
            Closing += new System.ComponentModel.CancelEventHandler(BaseWindow_Closing);
            Loaded += new RoutedEventHandler(BaseWindow_Loaded);
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取当前窗口句柄
        /// </summary>
        public IntPtr WndHandle
        {
            get
            {
                return wndHandle;
            }
        }

        /// <summary>
        /// 获取或设置当前窗口是否正在处理事务
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return isBusy;
            }
            set
            {
                isBusy = value;
            }
        }

        /// <summary>
        /// 获取或设置窗口类型标识符。
        /// </summary>
        public int WindowType
        {
            get;
            set;
        }

        /// <summary>
        /// 是否正在关闭窗口
        /// </summary>
        public bool ClosingWindow
        {
            get;
            set;
        }

        /// <summary>
        /// 是否为设计模式
        /// </summary>
        public bool IsInDesignModel
        {
            get { return System.ComponentModel.DesignerProperties.GetIsInDesignMode(this); }
        }

        /// <summary>
        /// 该对象是否已被释放
        /// </summary>
        public bool Disposed
        {
            get;
            set;
        }

        #endregion

        #region 公共、保护方法

        #region IDisposable 成员

        public void Dispose()
        {
            GC.SuppressFinalize(this);
            if (!Disposed)
            {
                Dispose(true);
            }
            Disposed = true;
        }

        /// <summary>
        /// 释放托管和非托管资源
        /// </summary>
        /// <param name="disposing">true：清理托管和非托管资源；false：清理非托管资源</param>
        protected virtual void Dispose(bool disposing)
        {
        }

        #endregion

        #endregion

        #region 事件方法

        void BaseWindow_Loaded(object sender, RoutedEventArgs e)
        {
            wndHandle = new WindowInteropHelper(this).EnsureHandle();
        }

        void BaseWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ClosingWindow = true;
        }

        void BaseWindow_Closed(object sender, EventArgs e)
        {
            ClosingWindow = false;
            Dispose();
        }

        #endregion      

        ~MessageBaseWindow()
        {
            Dispose(false);
        }
    }
}
